package com.example.back.service.impl;

import com.example.back.dto.req.DoctorNoticeEditReq;
import com.example.back.dto.req.DoctorNoticeListReq;
import com.example.back.dto.resp.DoctorNoticeListResp;
import com.example.back.jpa.AdminRepository;
import com.example.back.jpa.DoctorNoticeRepository;
import com.example.back.mapper.DoctorNoticeMapper;
import com.example.back.service.DoctorNoticeService;
import com.example.back.utils.UserUtils;
import com.example.common.entity.Admin;
import com.example.common.entity.HDoctorNotice;
import com.example.common.entity.Keys;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.example.common.utils.IdUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 信息发布
 */
@Service
public class DoctorNoticeServiceImpl implements DoctorNoticeService {

    @Resource
    private DoctorNoticeMapper doctorNoticeMapper;
    @Resource
    private DoctorNoticeRepository doctorNoticeRepository;
    @Resource
    private AdminRepository adminRepository;
    @Resource
    private UserUtils userUtils;

    /**
     * 获取信息发布列表
     */
    @Override
    public PageInfo<DoctorNoticeListResp> list(DoctorNoticeListReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));

        //分页页码，默认1页
        int pageNumber = null == req.getPage() ? 1 : req.getPage();
        //分页条数，默认10条
        int pageSize = null == req.getLimit() ? 10 : req.getLimit();
        //开始时间
        Date startTime = req.getStartTime();
        //结束时间
        Date EndTime = req.getEndTime();
        //操作类型
        Byte  nType = req.getNType();

        int types = 4;
        return PageHelper
                .startPage(pageNumber, pageSize)
                .doSelectPageInfo(() -> doctorNoticeMapper.findDoctorNoticeByCreateTime(startTime,EndTime,nType));
    }

    /**
     * 修改和添加信息发布
     */
    @Override
    @JaversAuditable
    public Object commitSave(DoctorNoticeEditReq req) {

        CheckUtils.checkNull(req, new ApiException(1, "对象不能为空"));
        Admin admin = adminRepository.findById(userUtils.getUser().getId()).orElse(null);

        if(req.getId() == 0){

            HDoctorNotice hdoctorNotice = new HDoctorNotice();
            hdoctorNotice.setId(IdUtils.generateId());
            hdoctorNotice.setContents(req.getContents());
            hdoctorNotice.setCreateTime(new Date());
            hdoctorNotice.setIsTop(1);
            hdoctorNotice.setNType(req.getNType());
            hdoctorNotice.setCreateName(admin.getName());

            return doctorNoticeRepository.save(hdoctorNotice);
        }else{
            HDoctorNotice objs = doctorNoticeRepository.findById(req.getId()).orElse(null);
            if(objs == null){
                throw new ApiException(1, "错误的请求参数");
            }

            objs.setContents(req.getContents());
            objs.setNType(req.getNType());
            objs.setCreateName(admin.getName());
            return doctorNoticeRepository.save(objs);
        }

    }

    /**
     * 删除信息发布
     */
    @Override
    @JaversAuditable
    public Object commitDel(DoctorNoticeEditReq req) {
        HDoctorNotice objs = doctorNoticeRepository.findById(req.getId()).orElse(null);
        if(objs == null){
            throw new ApiException(1, "错误的请求参数");
        }
        doctorNoticeRepository.deleteById(req.getId());
        return null;
    }
}
